package com.haiheng.module_main.adapter.bookcar

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.viewbinding.ViewBinding
import com.google.android.material.internal.ViewUtils.dpToPx
import com.haiheng.library_base.adapter.BaseAdapter
import com.haiheng.library_base.toast.SizeUtils
import com.haiheng.library_base.view.shape.ShadowDrawable
import com.haiheng.module_main.R
import com.haiheng.module_main.bean.BookDeliveryOrderStatus
import com.haiheng.module_main.bean.res.bookcar.BookCarBookResult
import com.haiheng.module_main.bean.res.bookcar.BookDeliveryOrderBean
import com.haiheng.module_main.databinding.ItemBookDeliveryOrderBinding

/**
 *
 * @Des:
 * @Author: hwz
 * @Date:2024/8/28 0028
 * @Version: 1.0
 */
class BookDeliveryOrderAdapter: BaseAdapter<BookDeliveryOrderBean>()  {

    init {
        addOnClickListener(R.id.slDeliveryBook)
        addOnClickListener(R.id.slNotDeliveryBook)
    }
    override fun onCreateViewBinding(viewType: Int): (LayoutInflater, ViewGroup, Boolean) -> ViewBinding {
        return ItemBookDeliveryOrderBinding::inflate
    }


    override fun onItemView(holder: ViewBindHolder, position: Int, item: BookDeliveryOrderBean) {
        val binding = holder.binding as ItemBookDeliveryOrderBinding
        binding.tvDeskNum.setText(item.deskBarcode)
        binding.tvBookName.setText("${item.items[0].title}")
        binding.tvISBN.setText("${item.items[0].classNumber ?: ""}")
        binding.tvPhoneNum.setText(item.phoneNumber)
        binding.tvOrderTime.setText(item.creationTime)
        if (item.loaction.isNullOrEmpty()){
            binding.tvLocation.setText("")
        }else{

            var location = ""
            item.loaction.withIndex().forEach { (index, itemLocation) ->
                location += itemLocation
                // 只有在不是最后一个元素时才添加换行符
                if (index != item.loaction.lastIndex) {
                    location += "\n"
                }
            }

            binding.tvLocation.setText(location)
        }

//        binding.tvOrderName.setText(item.creatorId)
        ShadowDrawable.setShadowDrawable(binding.carview,
            holder.itemView.context.getColor(R.color.white),
            SizeUtils.dp2px(10f),
            holder.itemView.context.getColor(R.color.color_330071FF)
            ,SizeUtils.dp2px(4f), 0, 0)

        when(item.bookDeliveryOrderStatus){
            BookDeliveryOrderStatus.PENDING.code->{
                binding.tvStatus.visibility = View.GONE
                binding.slDeliveryBook.visibility = View.VISIBLE
                binding.tvDeliveryBook.setText("送书")
                binding.slNotDeliveryBook.visibility = View.VISIBLE
            }
            BookDeliveryOrderStatus.DELIVERING.code->{
                binding.tvStatus.visibility = View.GONE
                binding.slDeliveryBook.visibility = View.VISIBLE
                binding.tvDeliveryBook.setText("处理中")
                binding.slNotDeliveryBook.visibility = View.VISIBLE
            }
            BookDeliveryOrderStatus.COMPLETED.code->{
                binding.tvStatus.visibility = View.VISIBLE
                binding.slDeliveryBook.visibility = View.GONE
                binding.slNotDeliveryBook.visibility = View.GONE
            }
            else->{
                binding.tvStatus.setText("已取消")
                binding.tvStatus.visibility = View.VISIBLE
                binding.slDeliveryBook.visibility = View.GONE
                binding.slNotDeliveryBook.visibility = View.GONE
            }
        }
        addOnClickListener(R.id.slDeliveryBook)
        addOnClickListener(R.id.slNotDeliveryBook)



    }
}